//
//  ProcessView.swift
//  WineComing
//
//  Created by xiaozhi on 15/8/3.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit

class ProcessView: UIView {
    var activeView:UIActivityIndicatorView!
    var noteLabel:UILabel!
    var messageLabel:UILabel!
    var bgView:UIView!
    let bgView_width:CGFloat = 80.0
    let bgView_height:CGFloat = 80.0
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    convenience init(customView:UIView){
        self.init(frame:customView.bounds)
        print(self.frame)
        bgView = UIView(frame: CGRectMake((bounds.size.width-bgView_width)/2.0, (bounds.size.height-bgView_height)/2.0, bgView_width, bgView_height))
        bgView.layer.cornerRadius = 10.0
        bgView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.8)
        self.addSubview(bgView)
        activeView = UIActivityIndicatorView(frame: CGRectMake((bgView_width - 30)/2.0, 15, 30, 30))
        activeView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
        bgView.addSubview(activeView)
        noteLabel = UILabel(frame: CGRectMake(0, CGRectGetMaxY(activeView.frame)+10, bgView_width, 20))
        noteLabel.font = UIFont.systemFontOfSize(14)
        noteLabel.textColor = UIColor.whiteColor()
        noteLabel.textAlignment = NSTextAlignment.Center
        bgView.addSubview(noteLabel)
        
        messageLabel = UILabel(frame: CGRectMake((bounds.size.width - bgView_width)/2.0, (bounds.size.height-30)/2.0, bgView_width, 30))
        messageLabel.layer.cornerRadius = 6.0
        messageLabel.layer.masksToBounds = true
        messageLabel.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.8)
        messageLabel.font = UIFont.systemFontOfSize(14)
        messageLabel.textColor = UIColor.whiteColor()
        messageLabel.textAlignment = NSTextAlignment.Center
        self.addSubview(messageLabel)
        messageLabel.hidden = true
    }
    func getMessageWidth(message:String?)->CGSize{
        if let textStr = message{
            let attString = NSMutableAttributedString(string: textStr)
            attString.addAttribute(NSFontAttributeName, value: messageLabel.font, range: NSMakeRange(0, attString.length))
            let formatSetter = CTFramesetterCreateWithAttributedString(attString)
            var range = CFRangeMake(0, attString.length)
            //            let path = CGPathCreateWithRect(CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height), nil)
            //            let frameRef = CTFramesetterCreateFrame(formatSetter, CFRangeMake(0, attString.length), path, nil)
            let reallySize = CTFramesetterSuggestFrameSizeWithConstraints(formatSetter, CFRangeMake(0, attString.length), nil, CGSizeMake(1000, 30), &range)
            return reallySize
        }
        return CGSizeZero
    }
    func showShortMessage(message:String?){
        self.hidden = false
        bgView.hidden = true
        messageLabel.hidden = false
         messageLabel.layer.cornerRadius = 6.0
        let size = self.getMessageWidth(message)
        messageLabel.frame = CGRectMake((bounds.size.width - size.width-16)/2.0, messageLabel.frame.origin.y, size.width+16, messageLabel.bounds.size.height)
        messageLabel.text = message
        superview?.bringSubviewToFront(self)
        let delaytime1:NSTimeInterval = 2.5
        let dipatchTime1:dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(delaytime1*Double(NSEC_PER_SEC)))
        dispatch_after(dipatchTime1, dispatch_get_main_queue()){ [weak self] in
            UIView.animateWithDuration(0.5, animations: {self?.alpha=0.0}, completion: {finishe in self?.hidden = true; self?.alpha=1.0})
        }
    }
    func showLongTimeMessage(message:String?){
        self.hidden = false
        bgView.hidden = true
        messageLabel.hidden = false
        messageLabel.layer.cornerRadius = 6.0
        let size = self.getMessageWidth(message)
        messageLabel.frame = CGRectMake((bounds.size.width - size.width-16)/2.0, messageLabel.frame.origin.y, size.width+16, messageLabel.bounds.size.height)
        messageLabel.text = message
        superview?.bringSubviewToFront(self)
        let delaytime1:NSTimeInterval = 3
        let dipatchTime1:dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(delaytime1*Double(NSEC_PER_SEC)))
        dispatch_after(dipatchTime1, dispatch_get_main_queue()){ [weak self] in
            UIView.animateWithDuration(0.5, animations: {self?.alpha=0.0}, completion: {finishe in self?.hidden = true; self?.alpha=1.0})
        }
    }
    func showProcessView(message:String?){
        self.hidden = false
        bgView.hidden = false
        messageLabel.hidden = true
        superview?.bringSubviewToFront(self)
        noteLabel.text = message
        activeView.startAnimating()
    }
    func hiddenProcessView(animation:Bool){
        if animation{
            UIView.animateWithDuration(0.5, animations: {self.alpha=0.0}, completion: {finishe in self.hidden = true; self.alpha=1.0; if self.activeView.isAnimating(){
                self.activeView.stopAnimating()
                }})
        }
        else{
            self.hidden = true
            self.alpha=1.0
            if self.activeView.isAnimating(){
                self.activeView.stopAnimating()
            }
        }

    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    deinit{
        print("deinit processView")
    }
}
